package com.gewu.pm.ui.activity.funds;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.FreezerDetailBean;
import com.gewu.pm.http.glide.GlideApp;
import com.gewu.pm.other.IntentKey;

import java.io.File;

/**
 * 冰柜详情页面
 * 
 * 功能：
 * 1. 展示冰柜完整信息
 * 2. 展示配送商信息
 * 3. 图片展示和删除功能
 * 4. 跳转到编辑页面
 */
public final class FreezerDetailActivity extends AppActivity {

    // 冰柜管理
    private TextView mEditButton;
    private TextView mNameplateCodeView;
    private TextView mFreezerNameView;
    private TextView mFreezerTypeView;
    private TextView mTerminalNameView;
    private TextView mTerminalCodeView;
    private TextView mHasSubsidyView;
    private TextView mIsPlacedView;
    private TextView mSubsidyAmountView;
    
    // 冰柜照片
    private ImageView mFreezerPhotoView;
    private String mFreezerPhotoPath;
    
    // 冰柜铭牌编码照片
    private ImageView mNameplatePhotoView;
    private String mNameplatePhotoPath;

    // 配送商信息
    private TextView mVehicleCodeView;
    private TextView mDeliveryProviderView;
    private TextView mSalespersonView;
    private TextView mSalesAreaView;

    private FreezerDetailBean mFreezerData;

    public static void start(Context context, String freezerId) {
        Intent intent = new Intent(context, FreezerDetailActivity.class);
        intent.putExtra(IntentKey.ID, freezerId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.freezer_detail_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        // 冰柜管理
        mEditButton = findViewById(R.id.btn_edit);
        mNameplateCodeView = findViewById(R.id.tv_nameplate_code);
        mFreezerNameView = findViewById(R.id.tv_freezer_name);
        mFreezerTypeView = findViewById(R.id.tv_freezer_type);
        mTerminalNameView = findViewById(R.id.tv_terminal_name);
        mTerminalCodeView = findViewById(R.id.tv_terminal_code);
        mHasSubsidyView = findViewById(R.id.tv_has_subsidy);
        mIsPlacedView = findViewById(R.id.tv_is_placed);
        mSubsidyAmountView = findViewById(R.id.tv_subsidy_amount);
        
        // 冰柜照片
        mFreezerPhotoView = findViewById(R.id.iv_freezer_photo);

        // 冰柜铭牌编码照片
        mNameplatePhotoView = findViewById(R.id.iv_nameplate_photo);

        // 配送商信息
        mVehicleCodeView = findViewById(R.id.tv_vehicle_code);
        mDeliveryProviderView = findViewById(R.id.tv_delivery_provider);
        mSalespersonView = findViewById(R.id.tv_salesperson);
        mSalesAreaView = findViewById(R.id.tv_sales_area);

        // 设置点击事件
        setOnClickListener(mEditButton);
    }

    @Override
    protected void initData() {
        String freezerId = getString(IntentKey.ID);
        // TODO: 加载冰柜详情数据
        loadFreezerDetail(freezerId);
    }

    /**
     * 加载冰柜详情
     */
    private void loadFreezerDetail(String freezerId) {
        // TODO: 网络请求加载数据
        // 模拟数据
        mFreezerData = new FreezerDetailBean();
        mFreezerData.setId(freezerId);
        mFreezerData.setFreezerName("终端-1876a-1——冰柜1");
        mFreezerData.setFreezerNameplateCode("6789566");
        mFreezerData.setFreezerType("类型名称1");
        mFreezerData.setTerminalName("终端-1876a-1");
        mFreezerData.setTerminalCode("6789566");
        mFreezerData.setSubsidized(true);
        mFreezerData.setPlaced(true);
        mFreezerData.setSubsidyAmount("300");
        mFreezerData.setDeliveryProvider("杭州小配送18号车");
        mFreezerData.setVehicleCode("566789045");
        mFreezerData.setSalesperson("陈宏伟");
        mFreezerData.setSalesArea("杭州→滨江");
        mFreezerData.setFreezerPhotoPath("https://static.wikia.nocookie.net/onepiece/images/b/b6/Sanji_Anime_Post_Timeskip_Infobox.png/revision/latest?cb=20141008162600");
        mFreezerData.setNameplatePhotoPath("https://static.wikia.nocookie.net/onepiece/images/6/6d/Monkey_D._Luffy_Anime_Post_Timeskip_Infobox.png/revision/latest?cb=20141008161929");
        
        updateUI();
    }

    /**
     * 更新界面
     */
    private void updateUI() {
        if (mFreezerData == null) return;
        
        mFreezerNameView.setText(mFreezerData.getFreezerName());
        mNameplateCodeView.setText(mFreezerData.getFreezerNameplateCode());
        mFreezerTypeView.setText(mFreezerData.getFreezerType());
        mTerminalNameView.setText(mFreezerData.getTerminalName());
        mTerminalCodeView.setText(mFreezerData.getTerminalCode());
        mHasSubsidyView.setText(mFreezerData.isSubsidized() ? "是" : "否");
        mIsPlacedView.setText(mFreezerData.isPlaced() ? "是" : "否");
        mSubsidyAmountView.setText(mFreezerData.getSubsidyAmount() + "元");
        
        mVehicleCodeView.setText(mFreezerData.getVehicleCode());
        mDeliveryProviderView.setText(mFreezerData.getDeliveryProvider());
        mSalespersonView.setText(mFreezerData.getSalesperson());
        mSalesAreaView.setText(mFreezerData.getSalesArea());
        
        // 加载照片
        updateFreezerPhoto();
        updateNameplatePhoto();
    }

    /**
     * 更新冰柜照片
     */
    private void updateFreezerPhoto() {
        if (mFreezerData != null && !TextUtils.isEmpty(mFreezerData.getFreezerPhotoPath())) {
            mFreezerPhotoPath = mFreezerData.getFreezerPhotoPath();
            GlideApp.with(this)
                    .load(new File(mFreezerPhotoPath))
                    .placeholder(R.drawable.test_1)
                    .error(R.drawable.test_1)
                    .into(mFreezerPhotoView);
        } else {
            mFreezerPhotoView.setImageResource(R.drawable.add_ic);
        }
    }

    /**
     * 更新铭牌照片
     */
    private void updateNameplatePhoto() {
        if (mFreezerData != null && !TextUtils.isEmpty(mFreezerData.getNameplatePhotoPath())) {
            mNameplatePhotoPath = mFreezerData.getNameplatePhotoPath();
            GlideApp.with(this)
                    .load(new File(mNameplatePhotoPath))
                    .placeholder(R.drawable.test_1)
                    .error(R.drawable.test_1)
                    .into(mNameplatePhotoView);
        } else {
            mNameplatePhotoView.setImageResource(R.drawable.add_ic);
        }
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mEditButton) {
            // 跳转到编辑页面
            FreezerEditActivity.startForEdit(this, mFreezerData);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放资源
    }
}
